// src/api/client.js
import axios from 'axios'

/**
 * 获取所有客户列表
 * @returns {Promise<Client[]>}
 */
export function listClients() {
    return axios({
        url: '/client/list',
        method: 'GET'
    })
}

/**
 * 获取所有类型为“护理”且未分配 careLevelId 的客户列表
 * 可选按姓名模糊查询
 * @param {string} [name] 客户姓名关键字
 * @returns {Promise<Client[]>}
 */
export function listUnassignedCareClients(name = '', page = 1, size = 10) {
    const params = { page, size }
    if (name) params.name = name.trim()
    return axios
        .get('/client/unassigned', { params })
}

/**
 * 根据 ID 获取单个客户
 * @param {number|string} id 客户 ID
 * @returns {Promise<Client>}
 */
export function getClient(id) {
    return axios({
        url: `/client/${id}`,
        method: 'GET'
    })
}

/**
 * 创建新客户
 * @param {Object} client 客户对象
 * @returns {Promise<void>}
 */
export function createClient(client) {
    return axios({
        url: '/client/create',
        method: 'POST',
        data: client
    })
}

// /**
//  * 更新已有客户
//  * @param {Object} client 客户对象，必须包含 clientId
//  * @returns {Promise<void>}
//  */
// export function updateClient(client) {
//     return axios({
//         url: '/client/update',
//         method: 'PUT',
//         data: client
//     })
// }

/**
 * 分配服务对象给某个管家
 * @param {number|string} clientId
 * @param {number|string} adminId
 */
export function assignClientToAdmin(clientId, adminId) {
    return axios({
        url: '/client/update',
        method: 'PUT',
        data: {
            clientId,        // 客户主键
            careAdminId: adminId  // 要绑定给哪个管理员
        }
    })
}

// /**
//  * 删除客户
//  * @param {number|string} id 客户 ID
//  * @returns {Promise<void>}
//  */
// export function deleteClient(id) {
//     return axios({
//         url: `/client/delete/${id}`,
//         method: 'DELETE'
//     })
// }

/**
 * 修改客户的 careLevelId
 * @param {number|string} clientId 客户 ID
 * @param {number|string} careLevelId 护理级别 ID
 * @returns {Promise<void>}
 */
export function updateCareLevelId(clientId, careLevelId) {
    return axios({
        url: '/client/updateCareLevelId',
        method: 'POST',
        params: { clientId, careLevelId }
    })
}

/**
 * 移除客户的 careLevelId
 * @param {number|string} clientId 客户 ID
 * @returns {Promise<void>}
 */
export function deleteCareLevelId(clientId) {
    return axios({
        url: '/client/deleteCareLevelId',
        method: 'POST',
        params: { clientId }
    })
}

export function listWithoutMealSet(name = '') {
    const params = {}
    if (name) params.name = name
    return axios.get('/client/listWithoutMealSet', { params })
}



/**
 * 获取客户列表
 */
export function clientList(params) {
    const { pageNum, pageSize, query } = params;
    const url = `/client/listAll?pageNum=${pageNum}&pageSize=${pageSize}`;
    return axios({
        url: url,
        method: 'POST',
        data: query
    });
}
/**
 * 逻辑删除单个客户
 * @param {number|string} clientId 客户ID
 * @returns {Promise<ResponseBean<void>>}
 */
export function deleteClient(clientId) {
    return axios({
        url: `/client/deleteSingle/${clientId}`,
        method: 'DELETE'
    });
}

/**
 * 批量逻辑删除客户
 * @param {Array<number|string>} clientIds 客户ID数组
 * @returns {Promise<ResponseBean<void>>}
 */
export function batchDeleteClients(clientIds) {
    return axios({
        url: '/client/batch-delete',
        method: 'POST',
        data: clientIds // 将ID数组作为请求体发送
    });
}

/**
 * 恢复(撤销删除)单个客户
 * @param {number|string} clientId 客户ID
 * @returns {Promise<ResponseBean<void>>}
 */
export function restoreClient(clientId) {
    return axios({
        url: `/client/restore/${clientId}`,
        method: 'POST'
    });
}

//客户入住
export function submitCheckIn(checkInData) {
    return axios({
        url: '/api/registration/check-in',
        method: 'POST',
        data: checkInData
    });
}

/**
 * 获取单个客户的详细信息，用于编辑
 */
export function getClientDetails(clientId) {
    return axios({
        url: `/client/huixian/${clientId}`,
        method: 'GET'
    });
}

/**
 * 更新客户信息
 * @param {Object} updateData 包含clientId和所有更新字段的对象
 */
export function updateClient(updateData) {
    return axios({
        url: '/client/updateClient',
        method: 'PUT',
        data: updateData
    });
}

/**
 * 获取当前健康管家负责的客户列表
 * @param {Object} params - { pageNum, pageSize, carerId, clientName }
 */
export function getMyManagedClients(params) {
    return axios({
        url: '/client/my-clients',
        method: 'GET',
        params: params // GET请求使用params传递参数
    });
}

/**
 * 导出所有客户信息的Excel文件
 * @returns {Promise<Blob>} 返回一个二进制的Blob对象
 */
export async function exportClients() {
    return axios({
        url: '/client/export',
        method: 'GET',
        responseType: 'blob'
    });
}